<?php

class guardActions extends sfActions {

    public function executeSignin($request) {
        $user = $this->getUser();
        if ($user->isAuthenticated()) {
            return $this->redirect('@homepage');
        }

        $this->form = new yougoFormSignin();
        if ($request->isMethod('post')) {
            $this->form->bind($request->getParameter('signin'));
            if ($this->form->isValid()) {
                $values = $this->form->getValues();
                $this->getUser()->signin($values['user'], $values['basicAuth']);

                return $this->redirect($request->getReferer());
            }
        }
    }

    public function executeSignout($request) {
        $this->getUser()->signOut();
        return $this->redirect($request->getReferer());
    }

    public function executeSecure($request) {
        $this->getResponse()->setStatusCode(403);
    }

    public function executeChangePassword($request) {
        $this->form = new yougoChangePasswordForm();

        if ($request->isXmlHttpRequest())
            $this->setLayout('ajaxLayout');
    }

    public function executeUpdatePassword($request) {
        $this->form = new yougoChangePasswordForm();

        $formOk = $this->processFormUpdatePassword($request, $this->form);
        if ($request->isXmlHttpRequest()) {
            if ($formOk === true) {
                $this->getResponse()->setContentType('application/json');
                $json = json_encode(array(
                            'response' => 'OK',
                            'flashNotice' => $this->getUser()->getFlash('notice')));
                $this->getUser()->setFlash('notice', '');
                return $this->renderText($json);
            }
            if ($formOk === false) {
                $this->getResponse()->setContentType('application/json');
                $json = json_encode(array(
                            'response' => 'NOK',
                            'flashError' => $this->getUser()->getFlash('error')));
                $this->getUser()->setFlash('error', '');
                return $this->renderText($json);
            }
            $this->setLayout('ajaxLayout');
        }

        $this->setTemplate('changePassword');
    }

    protected function processFormUpdatePassword(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            try {
                UserRestResource::singleton()->updatePassword($this->getUser()->getId(), $form->getValues());
                $this->getUser()->updatePassword($form->getValue('newPassword'));
                $this->getUser()->setFlash('notice', 'Password changed successfully.');

                if ($request->isXmlHttpRequest())
                    return true;
            } catch (YougoException $e) {
                $this->getUser()->setFlashError();
                if ($request->isXmlHttpRequest())
                    return false;
            }

            $this->redirect('@homepage');
        }
    }

}
